const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require("terser-webpack-plugin");

module.exports = {
  entry: { path: ["regenerator-runtime/runtime", "./index.js"] },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "angulardemo.js",
    library: "angulardemo",
    libraryTarget: "umd",
    umdNamedDefine: true,
    publicPath: "http://localhost:9004",
  },
  module: {
    rules: [
      {
        test: /\.js(|x)$/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"],
          },
        },
      },
      {
        test: /\.(c|sc)ss$/,
        use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: {
          loader: "url-loader",
        },
      },
    ],
  },
  optimization: {
    splitChunks: false,
    minimize: true,
    minimizer: [
      new TerserPlugin({
        terserOptions: {
          // Terser的压缩选项
          compress: {
            // 压缩选项
            drop_console: true, // 移除console.*
            drop_debugger: true, // 移除debugger
            pure_funcs: ["console.log"], // 移除特定的函数，如console.log
          },
          ecma: 5,
          mangle: true,
          output: {
            comments: false, // 去掉注释内容
          },
          sourceMap: false, // 禁用源映射生成
        },
      }),
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./public/index.html",
    }),

    new MiniCssExtractPlugin({
      filename: "[name].css",
    }),
  ],
  devServer: {
    headers: { "Access-Control-Allow-Origin": "*" },
    // static: {
    //   directory: path.join(__dirname, 'dist'),
    // },
    contentBase: path.join(__dirname, "dist"),
    compress: true,
    port: 9004,
    historyApiFallback: true, // 用户尝试访问一个非存在的静态资源时，webpack-dev-server 会重定向到指定的页面
    hot: true,
  },
};
